package com.wzl.controller;

import com.wzl.common.MessageConst;
import com.wzl.common.entity.Result;
import com.wzl.common.pojo.UserPojo;
import com.wzl.service.MbBatchInsertService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * mybatis批量插入数据
 *
 * @author weizhongli
 * @since 2022-08-29 16:12
 */
@RestController
public class MbBatchInsertController {

    @Resource
    private MbBatchInsertService mbBatchInsertService;

    /**
     * 测试根据id查询
     */
    @RequestMapping("selectById/{id}")
    public Result selectById(@PathVariable("id") int id){
        try {
            UserPojo userPojo = mbBatchInsertService.selectById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS,userPojo);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 批量插入测试----普通集合插入
     */
    @RequestMapping("add1")
    public Result batchAdd1(){
        try {
            int rows = mbBatchInsertService.batchAdd1();
            return new Result(true, MessageConst.BATCH_SUCCESS,rows);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 批量插入测试----循环单条插入
     */
    @RequestMapping("add2")
    public Result batchAdd2(){
        try {
            int rows = mbBatchInsertService.batchAdd2();
            return new Result(true, MessageConst.BATCH_SUCCESS,rows);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 批量插入测试----循环单条插入 BATCH模式
     */
    @RequestMapping("add3")
    public Result batchAdd3(){
        try {
            int rows = mbBatchInsertService.batchAdd3();
            return new Result(true, MessageConst.BATCH_SUCCESS,rows);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 批量插入测试----循环单条插入 提前准备好集合 BATCH模式
     */
    @RequestMapping("add4")
    public Result batchAdd4(){
        try {
            int rows = mbBatchInsertService.batchAdd4();
            return new Result(true, MessageConst.BATCH_SUCCESS,rows);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 批量插入测试----foreach分批次插入  速度最快
     * @return
     */
    @RequestMapping("add5")
    public Result batchAdd5(){
        try {
            int rows = mbBatchInsertService.batchAdd5();
            return new Result(true, MessageConst.BATCH_SUCCESS,rows);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }


    /**
     *  批量插入测试----多线程 thread
     */
    @RequestMapping("add6")
    public Result batchAdd6(){
        try {
            int rows = mbBatchInsertService.batchAdd6();
            return new Result(true, MessageConst.BATCH_SUCCESS,rows);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }


    /**
     *  批量插入测试----多线程 callable
     */
    @RequestMapping("add7")
    public Result batchAdd7(){
        try {
            int rows = mbBatchInsertService.batchAdd7();
            return new Result(true, MessageConst.BATCH_SUCCESS,rows);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

}